// 载入插件
var gulp = require('gulp'),  
htmlmin = require('gulp-htmlmin'),				//html压缩
fileinclude = require('gulp-file-include'),     //html片段引入
sass = require('gulp-sass'),					//sass编译
autoprefixer = require('gulp-autoprefixer'),	//css前缀
minifycss = require('gulp-minify-css'),			//css压缩
cssBase64 = require('gulp-css-base64'),			//css·base64转码
babel = require("gulp-babel"),                  //es6转化es5
uglify = require('gulp-uglify'),				//js压缩
imagemin = require('gulp-imagemin'),			//img压缩
rev = require('gulp-rev'),						//添加版本号
revCollector = require('gulp-rev-collector'),	//替换引用路径
sourcemaps = require('gulp-sourcemaps'),        //文件sourcemaps
plumber= require('gulp-plumber'),				//任务错误捕捉
clear = require('gulp-clean'),					//清理档案
fontSpider = require( 'gulp-font-spider' );     //字体转换压缩

// 项目目录
var proArr=["bella"];

// 当前项目
var curPro=proArr[0];

// doc
gulp.task('doc', function() {  
return gulp.src([curPro+'/src/doc/**/*.html',"!"+curPro+'/src/doc/{public,layer}/*.html'])
.pipe(plumber())
.pipe(fileinclude({prefix:'@',basepath:'@file'}))
.pipe(gulp.dest(curPro+'/src/html'))
});

// sass
gulp.task('sass', function() {
return gulp.src([curPro+'/src/sass/**/*.scss',"!"+curPro+'/src/sass/{common,public,layer}/*.scss'])
.pipe(plumber())
.pipe(autoprefixer({browsers: ['>5%']}))
.pipe(sass().on('error', sass.logError))
.pipe(gulp.dest(curPro+'/src/css'))
});

// babel
gulp.task('babel', function() {
return gulp.src(curPro+'/src/babel/**/*.js')
.pipe(plumber())
.pipe(babel({ 
    presets: ['es2015', 'es2016', 'es2017'], 
    plugins: [["transform-runtime", {
    "polyfill": false, 
    "regenerator": true 
    }]] 
}))
.pipe(gulp.dest(curPro+'/src/js'))
});

// html
gulp.task('html',['clear-html'], function() {  
return gulp.src(curPro+'/src/html/**/*.html')
.pipe(plumber())
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest(curPro+'/dist/html'))
});



// css
gulp.task('css',['clear-css'], function() {  
return gulp.src(curPro+'/src/css/**/*.css')
.pipe(plumber())
.pipe(cssBase64())
.pipe(minifycss())
.pipe(rev())
.pipe(gulp.dest(curPro+'/dist/css'))
.pipe(rev.manifest())
.pipe(gulp.dest(curPro+'/dist/rev/css'))
});

// js
gulp.task('js',['clear-js'],function() {  
return gulp.src([curPro+'/src/js/**/*.js'])
.pipe(plumber())
.pipe(uglify())
.pipe(rev())
.pipe(gulp.dest(curPro+'/dist/js'))
.pipe(rev.manifest())
.pipe(gulp.dest(curPro+'/dist/rev/js'))
})

// image
gulp.task('img',['clear-img'], function() {  
return gulp.src(curPro+'/src/img/**/*')
  .pipe(plumber())
// .pipe(imagemin())
// .pipe(rev())
.pipe(gulp.dest(curPro+'/dist/img'))
// .pipe(rev.manifest())
// .pipe(gulp.dest(curPro+'/dist/rev/image'))
});

// font
gulp.task('fonts',['clear-fonts'],function() {   
return gulp.src(curPro+'/src/html/mall/mall_index.html')  
.pipe(fontSpider())
.pipe(gulp.dest(curPro+'/dist/fonts'))

});  

// js-map
gulp.task('js-map',['clear-js'],function() {  
return gulp.src([curPro+'/src/js/**/*.js'])
.pipe(sourcemaps.init())
.pipe(plumber())
.pipe(uglify())
.pipe(sourcemaps.write())
.pipe(rev())
.pipe(gulp.dest(curPro+'/dist/js'))
.pipe(rev.manifest())
.pipe(gulp.dest(curPro+'/dist/rev/js'))
})

// html资源引用路径替换
gulp.task('rev-html', function() {   
return gulp.src([curPro+'/dist/rev/**/*.json',curPro+'/dist/html/**/*.html'])  
.pipe(revCollector({replaceReved:true}))
.pipe(gulp.dest(curPro+'/dist/html'))
});  

// css字体图标资源引用路径替换
gulp.task('rev-css', function() {   
return gulp.src([curPro+'/dist/rev/**/*.json',curPro+'/dist/css/**/*.css'])  
.pipe(revCollector({replaceReved:true}))
.pipe(gulp.dest(curPro+'/dist/css'))
});  

// rev资源引用路径替换
gulp.task('rev',['rev-html','rev-css']);    

// 编译监控
gulp.task('watch', function() {
gulp.start(['doc','sass','babel']);
gulp.watch(curPro+'/src/doc/**/*.html',['doc']);
gulp.watch(curPro+'/src/sass/**/*.scss',['sass']);
gulp.watch(curPro+'/src/babel/**/*.js',['babel']);
});

// 发布版本-map
gulp.task('publish-map',['html','css','js-map','img','fonts'],function(){
 gulp.start('rev');
});

// 默认预设-map
gulp.task('map', ['doc','sass','babel'],function(){
 gulp.start('publish-map');
});

// 发布版本
gulp.task('publish',['html','css','js','img','fonts'],function(){
 gulp.start('rev');
});

// 默认预设
gulp.task('default', ['doc','sass','babel','fonts'],function(){
 gulp.start('publish');
});

// 清空html
gulp.task('clear-html', function() {  
return gulp.src([curPro+'/dist/html'], {read: false})
.pipe(clear())
});

// 清空css
gulp.task('clear-css', function() {  
return gulp.src([curPro+'/dist/css'], {read: false})
.pipe(clear())
});

// 清空js
gulp.task('clear-js', function() {  
return gulp.src([curPro+'/dist/js'], {read: false})
.pipe(clear())
});

// 清空image
gulp.task('clear-img', function() {  
return gulp.src([curPro+'/dist/img'], {read: false})
.pipe(clear())
});

// 清空font
gulp.task('clear-fonts', function() {  
return gulp.src([curPro+'/dist/fonts'], {read: false})
.pipe(clear())
});